/*
 * $Id$
 */
package py.windmill.web.article;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;

import py.windmill.util.StreamReader;

import static py.windmill.util.JsonUtil.*;

/**
 *
 * @author myang
 */
public class Articles {

    private static final Map<String, List<Article>> ARTICLES;

    static {
        ARTICLES = new StreamReader<Map<String, List<Article>>>() {
            @Override
            public Map<String, List<Article>> read(final InputStream input) throws Exception {
                final Map<String, List<Article>> arts = new LinkedHashMap();
                final JsonArray months =
                  JsonArray.readFrom(new InputStreamReader(input, "utf-8"));
                for (final JsonValue v : months) {
                    final String yyyyMM = string(v);
                    arts.put(yyyyMM, list(yyyyMM));
                }
                return arts;
            }
        }.readThenClose(Article.class.getResourceAsStream("articles.json"));
    }

    /**
     *
     * @param yyyyMM
     * @return
     */
    public static List<Article> list(final String yyyyMM) {
        final String fn = yyyyMM + ".json";
        return new StreamReader<List<Article>>() {
            @Override
            public List<Article> read(final InputStream input)
              throws Exception {
                final List<Article> articles = new ArrayList<Article>();
                final JsonObject pubs =
                  JsonObject.readFrom(new InputStreamReader(input, "utf-8"));
                for (final JsonObject.Member pub : pubs) {
                    for (final JsonObject.Member art : pub.getValue().asObject()) {
                        articles.add(toArticle(art));
                    }
                }
                return articles;
            }
        }.readThenClose(Article.class.getResourceAsStream(fn));
    }

    /**
     *
     * @return
     */
    public static String[] months() {
        return ARTICLES.keySet().toArray(new String[]{});
    }

    /**
     *
     * @param yyyyMM
     * @return
     */
    public static List<Article> get(final String yyyyMM) {
        return ARTICLES.get(yyyyMM);
    }

    /**
     *
     * @param yyyyMM
     * @param name
     * @return
     */
    public static Article read(final String yyyyMM, final String name) {
        for (final Article art : ARTICLES.get(yyyyMM)) {
            if (art.getName().equals(name)) {
                return new StreamReader<Article>() {
                    @Override
                    public Article read(final InputStream input) throws Exception {
                        return new Article(art, IOUtils.toString(input, "utf-8"));
                    }
                }.readThenClose(Articles.class.getResourceAsStream("_" + yyyyMM + "/" + name));
            }
        }
        return null;
    }

    private static Article toArticle(final JsonObject.Member mem) {
        final String title = mem.getName();
        final JsonObject obj = mem.getValue().asObject();
        final String summary = string(obj, "summary", "");
        final String name =
          string(obj, "name", title.toLowerCase().replace(' ', '-'));

        final Map<String, String> words = new LinkedHashMap<String, String>();
        final JsonArray w = array(obj, "words", new JsonArray());
        for (final JsonValue v : w) {
            final String k = string(v);
            if (null == k) {
                final JsonArray a = array(v);
                words.put(string(a, 1), string(a, 0));
            } else {
                words.put(k, k);
            }
        }
        return new Article().title(title).summary(summary).name(name).words(words);
    }

    /**
     *
     * @param array
     * @return
     */
    private static Article toArticle(final JsonObject obj) {
        final String title = string(obj, "title");
        final String summary = string(obj, "summary", "");
        final String name =
          string(obj, "name", title.toLowerCase().replace(' ', '-'));

        final Map<String, String> words = new LinkedHashMap<String, String>();
        final JsonArray w = array(obj, "words", new JsonArray());
        for (final JsonValue v : w) {
            final String k = string(v);
            if (null == k) {
                final JsonArray a = array(v);
                words.put(string(a, 1), string(a, 0));
            } else {
                words.put(k, k);
            }
        }
        return new Article().title(title).summary(summary).name(name).words(words);
    }

    /**
     *
     * @param args
     */
    public static void main(final String... args) {
        System.out.println(Articles.get("201403"));
        System.out.println(Articles.read("201403", "beat-the-press"));
    }
}
